/*     */ package com.zimbra.cs.index.elasticsearch;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.IndexStoreException;
/*     */ import java.io.IOException;
/*     */ import java.net.ConnectException;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.NoHttpResponseException;
/*     */ import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
/*     */ import org.apache.commons.httpclient.methods.RequestEntity;
/*     */ import org.apache.commons.httpclient.methods.StringRequestEntity;
/*     */ import org.json.JSONArray;
/*     */ import org.json.JSONException;
/*     */ import org.json.JSONObject;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ElasticSearchConnector
/*     */ {
/*  40 */   private int statusCode = -1;
/*  41 */   private String body = null;
/*     */   
/*     */ 
/*     */   public int executeMethod(HttpMethod method)
/*     */     throws IndexStoreException, IOException
/*     */   {
/*  47 */     String reqBody = "";
/*  48 */     if ((ZimbraLog.elasticsearch.isTraceEnabled()) && ((method instanceof EntityEnclosingMethod))) {
/*  49 */       EntityEnclosingMethod eem = (EntityEnclosingMethod)method;
/*  50 */       RequestEntity re = eem.getRequestEntity();
/*  51 */       if ((re instanceof StringRequestEntity)) {
/*  52 */         StringRequestEntity sre = (StringRequestEntity)re;
/*  53 */         reqBody = Strings.nullToEmpty(sre.getContent());
/*  54 */         if (reqBody.length() > 0) {
/*  55 */           reqBody = String.format("\nREQUEST BODY=%s", new Object[] { reqBody });
/*     */         }
/*     */       }
/*     */     }
/*     */     try {
/*  60 */       HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  61 */       this.statusCode = client.executeMethod(method);
/*     */     } catch (ConnectException ce) {
/*  63 */       throw new ZimbraElasticSearchDownException(ce);
/*     */ 
/*     */ 
/*     */     }
/*     */     catch (NoHttpResponseException nhre)
/*     */     {
/*     */ 
/*  70 */       throw new ZimbraElasticSearchNoResponseException(nhre);
/*     */     }
/*  72 */     this.body = method.getResponseBodyAsString();
/*  73 */     ZimbraLog.elasticsearch.trace("ElasticSearch request:%s %s - statusCode=%d%s\nRESPONSE BODY=%s", new Object[] { method.getName(), method.getURI(), Integer.valueOf(this.statusCode), reqBody, this.body });
/*     */     
/*  75 */     return this.statusCode;
/*     */   }
/*     */   
/*     */   public class ZimbraElasticSearchDownException extends IndexStoreException {
/*     */     private static final long serialVersionUID = -1564956672861500861L;
/*     */     
/*     */     public ZimbraElasticSearchDownException(Exception e) {
/*  82 */       super(e);
/*     */     }
/*     */   }
/*     */   
/*     */   public class ZimbraElasticSearchNoResponseException extends IndexStoreException {
/*     */     private static final long serialVersionUID = -1564956672861500861L;
/*     */     
/*     */     public ZimbraElasticSearchNoResponseException(Exception e) {
/*  90 */       super(e);
/*     */     }
/*     */   }
/*     */   
/*     */   private JSONObject getParentObject(String[] path) throws JSONException {
/*  95 */     JSONObject currObj = new JSONObject(this.body);
/*  96 */     for (int ndx = 0; ndx < path.length - 1; ndx++) {
/*  97 */       currObj = currObj.getJSONObject(path[ndx]);
/*  98 */       if (currObj == null) {
/*  99 */         ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */         
/* 101 */         return null;
/*     */       }
/*     */     }
/* 104 */     return currObj;
/*     */   }
/*     */   
/*     */   public JSONObject getJSONBody() throws JSONException {
/* 108 */     return new JSONObject(this.body);
/*     */   }
/*     */   
/*     */   public String getStringAtJsonPath(String[] path) {
/*     */     try {
/* 113 */       JSONObject parent = getParentObject(path);
/* 114 */       return parent == null ? null : parent.getString(path[(path.length - 1)]);
/*     */     } catch (JSONException e) {
/* 116 */       ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */     }
/* 118 */     return null;
/*     */   }
/*     */   
/*     */   public boolean getBooleanAtJsonPath(String[] path, boolean defaultValue)
/*     */   {
/*     */     try {
/* 124 */       JSONObject parent = getParentObject(path);
/* 125 */       return parent == null ? defaultValue : parent.getBoolean(path[(path.length - 1)]);
/*     */     } catch (JSONException e) {
/* 127 */       ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */     }
/* 129 */     return defaultValue;
/*     */   }
/*     */   
/*     */   public int getIntAtJsonPath(String[] path, int defaultValue)
/*     */   {
/*     */     try {
/* 135 */       JSONObject parent = getParentObject(path);
/* 136 */       return parent == null ? defaultValue : parent.getInt(path[(path.length - 1)]);
/*     */     } catch (JSONException e) {
/* 138 */       ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */     }
/* 140 */     return defaultValue;
/*     */   }
/*     */   
/*     */   public JSONObject getObjectAtJsonPath(String[] path)
/*     */   {
/*     */     try {
/* 146 */       JSONObject parent = getParentObject(path);
/* 147 */       return parent == null ? null : parent.getJSONObject(path[(path.length - 1)]);
/*     */     } catch (JSONException e) {
/* 149 */       ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */     }
/* 151 */     return null;
/*     */   }
/*     */   
/*     */   public JSONArray getArrayAtJsonPath(String[] path)
/*     */   {
/*     */     try {
/* 157 */       JSONObject parent = getParentObject(path);
/* 158 */       return parent == null ? null : parent.getJSONArray(path[(path.length - 1)]);
/*     */     } catch (JSONException e) {
/* 160 */       ZimbraLog.elasticsearch.error("Problem locating '%s' in JSON response body=%s", new Object[] { Joiner.on("/").skipNulls().join(path), this.body });
/*     */     }
/* 162 */     return null;
/*     */   }
/*     */   
/*     */   public static String actualUrl(String url)
/*     */   {
/* 167 */     if (!ZimbraLog.elasticsearch.isTraceEnabled()) {
/* 168 */       return url;
/*     */     }
/* 170 */     if (url.indexOf('?') > -1) {
/* 171 */       return String.format("%s&pretty", new Object[] { url });
/*     */     }
/* 173 */     return String.format("%s?pretty", new Object[] { url });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/elasticsearch/ElasticSearchConnector.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */